<?php

namespace App\Morefun\Quiz;

use App\Contracts\Repositories\Quiz\SheetRepository as SheetRepositoryContracts;
use App\Entities\QuizSheet;
use Illuminate\Support\Facades\DB;

class SheetRepository implements SheetRepositoryContracts
{
    public function batchCreate(array $sheets)
    {
        return QuizSheet::query()->insert($sheets);
    }

    public function countBy($activityId, $by)
    {
        $counts = QuizSheet::query()->select(['quiz_id', DB::raw('COUNT(*) AS total')])
            ->where('activity_id', $activityId)
            ->groupBy($by)
            ->get();

        return $counts;
    }

    public function findAllByActivity($activityId, array $columns = ['*'])
    {
        return QuizSheet::query()->where('activity_id', $activityId)->get($columns);
    }

    public function findAll(array $conditions, array $columns = ['*'])
    {
        return QuizSheet::query()->where($conditions)->get($columns);
    }

    public function findWithPaginate($perPage, array $conditions, array $columns = ['*'])
    {
        return QuizSheet::query()->select($columns)->where($conditions)->paginate($perPage);
    }

    public function find($id, array $columns = ['*'])
    {
        return QuizSheet::query()->where('id', $id)->first($columns);
    }

    public function delete($id)
    {
        return !!QuizSheet::destroy($id);
    }

    public function deleteBy($column, $value)
    {
        return QuizSheet::query()->where($column, $value)->delete();
    }
}
